package com.esteel.settlement.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.esteel.settlement.config.pa.PaTemplate;
import com.esteel.settlement.config.pa.PacketFactory;
import com.esteel.settlement.dao.SettAccountRepository;
import com.esteel.settlement.entity.TbSettAccount;
import com.esteel.settlement.entity.TbSettAccountPK;
import com.esteel.settlement.entity.TbSettFreeze;
import com.esteel.settlement.entity.TbSettFund;
import com.esteel.settlement.entity.TbSettFundChg;
import com.esteel.settlement.entity.TbSettFundPK;
import com.esteel.settlement.entity.TbSettHandfee;
import com.esteel.settlement.entity.TbSettInmoney;
import com.esteel.settlement.entity.TbSettOutmoney;
import com.esteel.settlement.entity.TbSettPlatInfo;
import com.esteel.settlement.pa.packet.Answer;
import com.esteel.settlement.pa.packet.CheckMemberAccountBankBalancePB1020;
import com.esteel.settlement.pa.packet.InMoneyPB1316;
import com.esteel.settlement.pa.packet.OutMoneyPB1318;
import com.esteel.settlement.pa.packet.PlatformChargeRefundPB1030;
import com.esteel.settlement.pa.packet.PlatformPayCollectPB1031;
import com.esteel.settlement.pa.packet.RegulatoryAccountInformationQueryPB1021;
import com.esteel.settlement.pa.packet.RspBP1020;
import com.esteel.settlement.pa.packet.RspBP1021;
import com.esteel.settlement.pa.packet.RspBP1029;
import com.esteel.settlement.pa.packet.RspBP1030;
import com.esteel.settlement.pa.packet.RspBP1031;
import com.esteel.settlement.pa.packet.RspBP1316;
import com.esteel.settlement.pa.packet.RspBP1318;
import com.esteel.settlement.pa.packet.SubAccountFreezeThawPB1029;
import com.esteel.settlement.service.SettAccountService;
import com.esteel.settlement.service.SettFreezeService;
import com.esteel.settlement.service.SettFundChgService;
import com.esteel.settlement.service.SettFundService;
import com.esteel.settlement.service.SettHandfeeService;
import com.esteel.settlement.service.SettInmoneyService;
import com.esteel.settlement.service.SettOutmoneyService;
import com.esteel.settlement.service.SettPlatInfoService;

@Service
public class SettAccountServiceImpl implements SettAccountService{

	@Autowired
	private SettAccountRepository settAccountRepository;
	

	@Autowired
	private SettFreezeService settFreezeService;
	
	@Autowired
	private SettFundService settFundService;
	
	@Autowired
	private SettFundChgService settFundChgService;
	
	@Autowired
	private SettHandfeeService settHandfeeService;
	
	@Autowired
	private SettInmoneyService settInmoneyService;
	
	@Autowired
	private SettOutmoneyService settOutmoneyService;
	
	@Autowired
	private SettPlatInfoService settPlatInfoService;
	
	@Autowired
	private PacketFactory packetFactory;

	@Autowired
	private PaTemplate paTemplate;
	
	
	
	@Override
	public TbSettAccount save(TbSettAccount tbSettAccount){
		return settAccountRepository.save(tbSettAccount);
	}

	@Override
	public Page<TbSettAccount> findAll(Pageable pageable) {
		
		Specification<TbSettAccount> spec = new Specification<TbSettAccount>(){

			@Override
			public Predicate toPredicate(Root<TbSettAccount> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				 Expression<String> custId = root.get("id").get("thirdCustId").as(String.class);
				 
				 List<Predicate> list = new ArrayList<Predicate>();
				 
				Predicate p = cb.equal(custId, "E00000062");
				list.add(p);
				Predicate p1 = cb.equal(root.get("idCode").as(String.class), "12343011-9");
			//	list.add(p1);
				Predicate[] predicates = new Predicate[list.size()];
				
//				return query.where(list.toArray(predicates)).getRestriction();
				return null;
			}};
		
		Page<TbSettAccount> page = settAccountRepository.findAll(spec, pageable);
		
//		Page<TbSettAccount> page = settAccountRepository.findAll(pageable);
		List<TbSettAccount> list = page.getContent();
		for (TbSettAccount account:list){
			account.setFund(settFundService.findByCustAcctId(account.getId().getCustAcctId()));
		}
		return page;
	}

	
	
	@Override
	public TbSettAccount findByCustAcctId(String custAcctId) {
		return settAccountRepository.findByCustAcctId(custAcctId);
	}

	
	@Override
	@Transactional
	public Answer saveFreeze(int funcFlag, String custAcctId, String thirdCustId, double tranAmount) {
		
		TbSettAccountPK pk = new TbSettAccountPK();
		pk.setCustAcctId(custAcctId);
		pk.setThirdCustId(thirdCustId);
		TbSettAccount account = settAccountRepository.findOne(pk);
		
		Assert.notNull(account, "没有查询到给定的客户信息");
		
		SubAccountFreezeThawPB1029 pb = packetFactory.getPacket(SubAccountFreezeThawPB1029.class);

	
		pb.setFuncFlag(funcFlag);
		pb.setCustAcctId(custAcctId);
		pb.setThirdCustId(thirdCustId);
		pb.setTranAmount(tranAmount);

		pb.setThirdHtId("1029_" + new Date().getTime());
		TbSettFreeze tbSettFreeze = new TbSettFreeze();
		BeanUtils.copyProperties(pb, tbSettFreeze);
		tbSettFreeze.setTranDate(pb.getTransDate());
		
		tbSettFreeze = settFreezeService.save(tbSettFreeze);
		
		pb.setThirdLogNo("1029_" +tbSettFreeze.getFrId());

		
		RspBP1029 rsp = paTemplate.execute(pb, RspBP1029.class);
		
		if (rsp.isSuccess()){
			
			//记录冻结解冻表的数据
			tbSettFreeze.setLogNo(rsp.getFrontLogNo());
			settFreezeService.save(tbSettFreeze);
			
			//记录平台端 资金账户信息
			TbSettFundPK fundPk = new TbSettFundPK();
			fundPk.setCustAcctId(custAcctId);
			fundPk.setThirdCustId(thirdCustId);
			TbSettFund fund = settFundService.findOne(fundPk);
			
			Assert.notNull(fund);
			
			//冻结资金
			if (funcFlag==SubAccountFreezeThawPB1029.FREEZE){
				fund.setTotalFreezeAmount(fund.getTotalFreezeAmount()+tranAmount);
				fund.setTotalBalance(fund.getTotalBalance()-tranAmount);
				
			}else if (funcFlag==SubAccountFreezeThawPB1029.UNFREEZE){
				fund.setTotalFreezeAmount(fund.getTotalFreezeAmount()-tranAmount);
				fund.setTotalBalance(fund.getTotalBalance()+tranAmount);
			}
			fund.setTranDate(rsp.getTransDate());
			fund = settFundService.save(fund);
			
			//记录资金变动信息
			
			TbSettFundChg settFundChg = new TbSettFundChg();
			BeanUtils.copyProperties(fund.getId(), settFundChg);
			BeanUtils.copyProperties(fund, settFundChg);
			settFundChg.setLinkType(funcFlag==SubAccountFreezeThawPB1029.FREEZE?TbSettFundChg.LinkType.FREEZE:TbSettFundChg.LinkType.UNFREEZE);
			settFundChg.setLinkKey(tbSettFreeze.getFrId());
			settFundChg.setTransDate(rsp.getTransDate());
			if (funcFlag==SubAccountFreezeThawPB1029.FREEZE){
				settFundChg.setChgTotalFreezeAmount(tranAmount);
				settFundChg.setChgTotalBalance(-tranAmount);
			}else if (funcFlag==SubAccountFreezeThawPB1029.UNFREEZE){
				settFundChg.setChgTotalFreezeAmount(-tranAmount);
				settFundChg.setChgTotalBalance(tranAmount);
			}
			
			settFundChgService.save(settFundChg);
			
		}

		
		return rsp;
		
	}

	@Override
	@Transactional
	public Answer saveHee(int funcFlag, String custAcctId, String thirdCustId, double tranAmount) {
		TbSettAccountPK pk = new TbSettAccountPK();
		pk.setCustAcctId(custAcctId);
		pk.setThirdCustId(thirdCustId);
		TbSettAccount account = settAccountRepository.findOne(pk);
		
		Assert.notNull(account, "没有查询到给定的客户信息");
		
		PlatformChargeRefundPB1030 pb = packetFactory.getPacket(PlatformChargeRefundPB1030.class);

		pb.setFuncFlag(funcFlag);
		pb.setCustAcctId(custAcctId);
		pb.setThirdCustId(thirdCustId);
		pb.setTranAmount(tranAmount);
		
		pb.setThirdHtId("1030_" + new Date().getTime());

		TbSettHandfee fee = new TbSettHandfee();
		BeanUtils.copyProperties(pb, fee);
		fee.setFeeType(TbSettHandfee.FEE);
		fee.setTranDate(pb.getTransDate());
		fee = settHandfeeService.save(fee);
		pb.setThirdLogNo("1030_"+fee.getId());
		
		
		RspBP1030 rsp = paTemplate.execute(pb, RspBP1030.class);
		
		//平安方 响应
		if (rsp.isSuccess()){
			
			//保存返回结果
			fee.setLogNo(rsp.getFrontLogNo());
			settHandfeeService.save(fee);
			

			//记录平台端 客户资金账户信息
			TbSettFundPK fundPk = new TbSettFundPK();
			fundPk.setCustAcctId(custAcctId);
			fundPk.setThirdCustId(thirdCustId);
			TbSettFund fund = settFundService.findOne(fundPk);
			
			TbSettFund charges = settFundService.findCharges();
			
			Assert.notNull(fund);
			Assert.notNull(charges);
			int linkedType=TbSettFundChg.LinkType.HANDFEEIN;
			//收费
			if (funcFlag==PlatformChargeRefundPB1030.HANDFEEIN){
				linkedType=TbSettFundChg.LinkType.HANDFEEIN;
				
				//减少子账户可用余额
				fund.setTotalBalance(fund.getTotalBalance()-tranAmount);
				//减少子账户总金额
				fund.setTotalAmount(fund.getTotalAmount()-tranAmount);
				
				//增加平台手续费账户可用余额
				charges.setTotalBalance(charges.getTotalBalance()+tranAmount);
				//增加平台手续费账户总金额
				charges.setTotalAmount(charges.getTotalAmount()+tranAmount);
				
			}else if (funcFlag==PlatformChargeRefundPB1030.HANDFEEOUT){//退费
				linkedType=TbSettFundChg.LinkType.HANDFEEOUT;
				//增加子账户可用余额
				fund.setTotalBalance(fund.getTotalBalance()+tranAmount);
				//增加子账户总金额
				fund.setTotalAmount(fund.getTotalAmount()+tranAmount);
				
				//减少平台手续费账户可用余额
				charges.setTotalBalance(charges.getTotalBalance()-tranAmount);
				//减少平台手续费账户总金额
				charges.setTotalAmount(charges.getTotalAmount()-tranAmount);
			}else if (funcFlag==PlatformChargeRefundPB1030.HANDFEEFREEZEIN){//冻结收费
				linkedType=TbSettFundChg.LinkType.HANDFEEFREEZEIN;
				//减少子账户冻结余额
				fund.setTotalFreezeAmount(fund.getTotalFreezeAmount()-tranAmount);
				//减少子账户总金额
				fund.setTotalAmount(fund.getTotalAmount()-tranAmount);
				
				//增加平台手续费账户可用余额
				charges.setTotalBalance(charges.getTotalBalance()+tranAmount);
				//增加平台手续费账户总金额
				charges.setTotalAmount(charges.getTotalAmount()+tranAmount);
			}
			fund = settFundService.save(fund);
			charges = settFundService.save(charges);
			
			//记录子账户资金变动信息
			TbSettFundChg settFundChg = new TbSettFundChg();
			BeanUtils.copyProperties(fund.getId(), settFundChg);
			BeanUtils.copyProperties(fund, settFundChg);
			settFundChg.setLinkType(linkedType);
			settFundChg.setLinkKey(fee.getId());
			settFundChg.setTransDate(rsp.getTransDate());
			if (funcFlag==1){
				//子账户费用减少
				settFundChg.setChgTotalBalance(-tranAmount);
			}else if (funcFlag==2){
				//子账户费用增加
				settFundChg.setChgTotalBalance(tranAmount);
			}else if(funcFlag==3){
				//冻结账户减少
				settFundChg.setChgTotalFreezeAmount(-tranAmount);
			}
			settFundChgService.save(settFundChg);
			
			//记录费用子账户资金变动信息
			TbSettFundChg settChargeFundChg = new TbSettFundChg();
			BeanUtils.copyProperties(charges.getId(), settChargeFundChg);
			BeanUtils.copyProperties(charges, settChargeFundChg);
			settChargeFundChg.setLinkType(linkedType);
			settChargeFundChg.setLinkKey(fee.getId());
			settChargeFundChg.setTransDate(fee.getTranDate());
			if (funcFlag==1){
				//费用子账户费用增加
				settChargeFundChg.setChgTotalBalance(tranAmount);
			}else if (funcFlag==2){
				//费用子账户费用减少
				settChargeFundChg.setChgTotalBalance(-tranAmount);
			}else if(funcFlag==3){
				//冻结账户减少
				settChargeFundChg.setChgTotalBalance(tranAmount);
			}
			settFundChgService.save(settChargeFundChg);
		}
		
		return rsp;
	}

	@Override
	@Transactional
	public Answer saveSett(int funcFlag, String custAcctId, String thirdCustId, double tranAmount) {
		TbSettAccountPK pk = new TbSettAccountPK();
		pk.setCustAcctId(custAcctId);
		pk.setThirdCustId(thirdCustId);
		TbSettAccount account = settAccountRepository.findOne(pk);
		
		Assert.notNull(account, "没有查询到给定的客户信息");
		
		PlatformPayCollectPB1031 pb = packetFactory.getPacket(PlatformPayCollectPB1031.class);
	
		pb.setFuncFlag(funcFlag);
		pb.setCustAcctId(custAcctId);
		pb.setThirdCustId(thirdCustId);
		pb.setTranAmount(tranAmount);
		
		pb.setThirdHtId("1031_" + new Date().getTime());
	
		TbSettHandfee fee = new TbSettHandfee();
		BeanUtils.copyProperties(pb, fee);
		fee.setFeeType(TbSettHandfee.SETT);
		fee.setTranDate(pb.getTransDate());
		fee = settHandfeeService.save(fee);
		pb.setThirdLogNo("1030_"+fee.getId());
		
		
		RspBP1031 rsp = paTemplate.execute(pb, RspBP1031.class);
		
		//平安方 响应
		if (rsp.isSuccess()){
			
			//保存返回结果
			fee.setLogNo(rsp.getFrontLogNo());
			settHandfeeService.save(fee);
			
	
			//记录平台端 客户资金账户信息
			TbSettFundPK fundPk = new TbSettFundPK();
			fundPk.setCustAcctId(custAcctId);
			fundPk.setThirdCustId(thirdCustId);
			TbSettFund fund = settFundService.findOne(fundPk);
			
			TbSettFund settFund = settFundService.findSettlement();
			
			Assert.notNull(fund);
			Assert.notNull(settFund);
			int linkedType=TbSettFundChg.LinkType.SETTIN;
			//会员支付到市场
			if (funcFlag==PlatformPayCollectPB1031.SETTIN){
				linkedType=TbSettFundChg.LinkType.SETTIN;
				
				//减少子账户可用余额
				fund.setTotalBalance(fund.getTotalBalance()-tranAmount);
				//减少子账户总金额
				fund.setTotalAmount(fund.getTotalAmount()-tranAmount);
				
				//增加市场清收账户可用余额
				settFund.setTotalBalance(settFund.getTotalBalance()+tranAmount);
				//增加市场清收账户总金额
				settFund.setTotalAmount(settFund.getTotalAmount()+tranAmount);
				
			}else if (funcFlag==PlatformPayCollectPB1031.SETTOUT){//市场支付到会员
				linkedType=TbSettFundChg.LinkType.SETTOUT;
				//增加子账户可用余额
				fund.setTotalBalance(fund.getTotalBalance()+tranAmount);
				//增加子账户总金额
				fund.setTotalAmount(fund.getTotalAmount()+tranAmount);
				
				//减少市场清收账户可用余额
				settFund.setTotalBalance(settFund.getTotalBalance()-tranAmount);
				//减少市场清收账户总金额
				settFund.setTotalAmount(settFund.getTotalAmount()-tranAmount);
			}else if (funcFlag==PlatformPayCollectPB1031.SETTFREEZEIN){//会员冻结支付到市场
				linkedType=TbSettFundChg.LinkType.SETTFREEZEIN;
				//减少子账户冻结余额
				fund.setTotalFreezeAmount(fund.getTotalFreezeAmount()-tranAmount);
				//减少子账户总金额
				fund.setTotalAmount(fund.getTotalAmount()-tranAmount);
				
				//增加市场清收账户可用余额
				settFund.setTotalBalance(settFund.getTotalBalance()+tranAmount);
				//增加市场清收账户总金额
				settFund.setTotalAmount(settFund.getTotalAmount()+tranAmount);
			}
			fund = settFundService.save(fund);
			settFund = settFundService.save(settFund);
			
			//记录子账户资金变动信息
			TbSettFundChg settFundChg = new TbSettFundChg();
			BeanUtils.copyProperties(fund.getId(), settFundChg);
			BeanUtils.copyProperties(fund, settFundChg);
			settFundChg.setLinkType(linkedType);
			settFundChg.setLinkKey(fee.getId());
			settFundChg.setTransDate(rsp.getTransDate());
			if (funcFlag==1){
				//子账户费用减少
				settFundChg.setChgTotalBalance(-tranAmount);
			}else if (funcFlag==2){
				//清收子账户费用增加
				settFundChg.setChgTotalBalance(tranAmount);
			}else if(funcFlag==3){
				//冻结账户减少
				settFundChg.setChgTotalFreezeAmount(-tranAmount);
			}
			settFundChgService.save(settFundChg);
			
			//记录清收子账户资金变动信息
			TbSettFundChg settChargeFundChg = new TbSettFundChg();
			BeanUtils.copyProperties(settFund.getId(), settChargeFundChg);
			BeanUtils.copyProperties(settFund, settChargeFundChg);
			settChargeFundChg.setLinkType(linkedType);
			settChargeFundChg.setLinkKey(fee.getId());
			settChargeFundChg.setTransDate(fee.getTranDate());
			if (funcFlag==1){
				//清收账户可用余额增加
				settChargeFundChg.setChgTotalBalance(tranAmount);
			}else if (funcFlag==2){
				//清收账户可用余额减少
				settChargeFundChg.setChgTotalBalance(-tranAmount);
			}else if(funcFlag==3){
				//清收账户可用余额增加
				settChargeFundChg.setChgTotalBalance(tranAmount);
			}
			settFundChgService.save(settChargeFundChg);
		}
		
		return rsp;
	}

	@Override
	@Transactional
	public Answer saveInMoney(String custAcctId, String thirdCustId, double tranAmount) {
		//查询子账户信息
		TbSettAccountPK pk = new TbSettAccountPK();
		pk.setCustAcctId(custAcctId);
		pk.setThirdCustId(thirdCustId);
		TbSettAccount account = settAccountRepository.findOne(pk);
		
		Assert.notNull(account, "没有查到账户信息");
		
		//查询入金账户余额
		CheckMemberAccountBankBalancePB1020 pb1020 =  packetFactory.getPacket(CheckMemberAccountBankBalancePB1020.class);

		pb1020.setThirdLogNo("1020_" + new Date().getTime());
		
		BeanUtils.copyProperties(account.getId(), pb1020);
		pb1020.setCustName(account.getCustName());
		pb1020.setAcctNo(account.getRelatedAcctId());
		
		RspBP1020 rsp1020 = paTemplate.execute(pb1020, RspBP1020.class);
		//如果查询错误 则终止入金
		if (!rsp1020.isSuccess()){
			return rsp1020;
		}
		
		Assert.isTrue(rsp1020.getBalance()>tranAmount, "账户可入金金额不足");
		
		//执行银行端入金操作
		InMoneyPB1316 pb1316 = packetFactory.getPacket(InMoneyPB1316.class);
		
		BeanUtils.copyProperties(account.getId(), pb1316);
		BeanUtils.copyProperties(account, pb1316);
		
		pb1316.setTranAmount(tranAmount);
		pb1316.setInAcctId(account.getRelatedAcctId());
		pb1316.setInAcctIdName(account.getAcctName());
		
		TbSettInmoney inMoney = new TbSettInmoney();
		
		BeanUtils.copyProperties(pb1316, inMoney);
		
		inMoney = settInmoneyService.save(inMoney);
		pb1316.setThirdLogNo("1316_"+inMoney.getInId());
		
		RspBP1316 rsp = paTemplate.execute(pb1316, RspBP1316.class);

		if (rsp.isSuccess()){
			//记录资金入库表
			inMoney.setAcctDate(rsp.getTransDate());
			inMoney.setLogNo(rsp.getFrontLogNo());
			settInmoneyService.save(inMoney);
			//修改账户资金信息
			TbSettFundPK fundPk = new TbSettFundPK();
			fundPk.setCustAcctId(custAcctId);
			fundPk.setThirdCustId(thirdCustId);
			TbSettFund fund = settFundService.findOne(fundPk);
			fund.setTotalAmount(fund.getTotalAmount()+tranAmount);
			fund.setTotalBalance(fund.getTotalBalance()+tranAmount);
			settFundService.save(fund);
			//增加账户流水信息
			TbSettFundChg settFundChg = new TbSettFundChg();
			BeanUtils.copyProperties(fund.getId(), settFundChg);
			BeanUtils.copyProperties(fund, settFundChg);
			settFundChg.setLinkType(TbSettFundChg.LinkType.IN);
			settFundChg.setLinkKey(inMoney.getInId());
			settFundChg.setTransDate(rsp.getTransDate());
			settFundChg.setChgTotalAmount(tranAmount);
			settFundChg.setChgTotalBalance(tranAmount);
			settFundChgService.save(settFundChg);
		}
		
		
		return rsp;
	}

	@Override
	public void delete(TbSettAccount tbSettAccount) {
		this.settAccountRepository.delete(tbSettAccount);
		
	}

	@Override
	public Answer saveOutMoney(String custAcctId, String thirdCustId, double tranAmount) {
		//查询子账户信息
		TbSettAccountPK pk = new TbSettAccountPK();
		pk.setCustAcctId(custAcctId);
		pk.setThirdCustId(thirdCustId);
		TbSettAccount account = settAccountRepository.findOne(pk);
		
		Assert.notNull(account, "没有查到账户信息");
		
		TbSettFund fund = settFundService.findByCustAcctId(custAcctId);
		
		//判断出金金额够不够
		Assert.isTrue(fund.getTotalBalance()>=tranAmount,"账户余额不足");
		
		
		//执行银行端入金操作
		OutMoneyPB1318 pb1318 = packetFactory.getPacket(OutMoneyPB1318.class);
		
		BeanUtils.copyProperties(account.getId(), pb1318);
		BeanUtils.copyProperties(account, pb1318);

		pb1318.setTranType(1);
		
		pb1318.setOutAcctId(account.getRelatedAcctId());
		pb1318.setOutAcctIdBankName(account.getBankName());
		pb1318.setOutAcctIdBankCode(account.getBankCode());
		pb1318.setOutAcctIdName(account.getCustName());
		
		pb1318.setTranAmount(tranAmount);

		
		TbSettOutmoney outMoney = new TbSettOutmoney();
		
		BeanUtils.copyProperties(pb1318, outMoney);
		
		
		
		outMoney = settOutmoneyService.save(outMoney);
		pb1318.setThirdLogNo("1316_"+outMoney.getOutId());
		
		RspBP1318 rsp = paTemplate.execute(pb1318, RspBP1318.class);

		if (rsp.isSuccess()){
			//记录资金入库表
			outMoney.setAcctDate(rsp.getTransDate());
			outMoney.setLogNo(rsp.getFrontLogNo());
			settOutmoneyService.save(outMoney);
			//修改账户资金信息
			fund.setTotalAmount(fund.getTotalAmount()-tranAmount);
			fund.setTotalBalance(fund.getTotalBalance()-tranAmount);
			settFundService.save(fund);
			//增加账户流水信息
			TbSettFundChg settFundChg = new TbSettFundChg();
			BeanUtils.copyProperties(fund.getId(), settFundChg);
			BeanUtils.copyProperties(fund, settFundChg);
			settFundChg.setLinkType(TbSettFundChg.LinkType.OUT);
			settFundChg.setLinkKey(outMoney.getOutId());
			settFundChg.setTransDate(rsp.getTransDate());
			settFundChg.setChgTotalAmount(-tranAmount);
			settFundChg.setChgTotalBalance(-tranAmount);
			settFundChgService.save(settFundChg);
		}
		
		
		return rsp;
	}

	@Override
	public TbSettAccount findByThirdCustId(String thirdCustId) {
		return settAccountRepository.findByThirdCustId(thirdCustId);
	}

	@Override
	public int isExistThirdCustId(String thirdCustId, String orgCode) {
		//判断给出的账号是否存在
		Long customerKey = settAccountRepository.thirdIdSize(thirdCustId);
		//账户不存在
		if (customerKey==null){
			return -1;
		}
		String baseOrgCode = settAccountRepository.getOrgCode(customerKey);
		if (baseOrgCode==null||!baseOrgCode.equals(orgCode)){
			return -2;
		}
		//验证组织机构代码证是否正确
		return 0;
	}

	@Override
	public RspBP1021 platInfo() {
		RegulatoryAccountInformationQueryPB1021 pb = packetFactory.getPacket(RegulatoryAccountInformationQueryPB1021.class);
//		pb.setTranFunc("1021");
		pb.setThirdLogNo("o" + new Date().getTime());
		RspBP1021 rsp = paTemplate.execute(pb, RspBP1021.class);

		TbSettPlatInfo tbSettPlatInfo = new TbSettPlatInfo();
		BeanUtils.copyProperties(rsp, tbSettPlatInfo);
		settPlatInfoService.save(tbSettPlatInfo);

		return rsp;
	}
	
	
}
